/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.idea.utils.sfo;


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JToolBar;
import net.java.dev.colorchooser.ColorChooser;
import org.openide.util.NbBundle.Messages;


@Messages(
{
    "# {0} - x" ,
    "# {1} - y" ,
    "LBL_Coords=Coords x: {0} , y: {1} "
} )
/**
 *
 * @author renke002
 */
public class ToolBar extends JToolBar implements ActionListener
{

    /**
     * Our color chooser component from the ColorChooser library.
     */
    private final ColorChooser colorChooser = new ColorChooser();
    /**
     *
     */
    private Canvas canvas;
    /**
     *
     */
    private JLabel mouseCoords;
    /**
     * Border visibility toggle.
     */
    private JButton borderToggle;
    /**
     * Track the border visibility for the items on the canvas.
     */
    private boolean borderVisible;
    /**
     * Border visibility toggle.
     */
    private JButton gridToggle;


    /**
     *
     */
    public ToolBar( TreeTopComponent parent )
    {
        this.setLayout( new FlowLayout( FlowLayout.LEFT , 7 , 7 ) );
        this.setFloatable( false );

        // initialize graphic components.
        this.initComponents();
    }


    /**
     *
     * @param e
     */
    @Override
    public void actionPerformed( ActionEvent e )
    {
        if ( e.getSource() instanceof ColorChooser )
        {
            ColorChooser cc = (ColorChooser) e.getSource();
            this.canvas.setBackGroundColor( cc.getColor() );
        } else if ( e.getSource() instanceof JButton )
        {
            String btnText = ( (JButton) e.getSource() ).getText();
            if ( btnText != null )
            {
                if ( btnText.startsWith( "Border" ) )
                {
                    // 
                } else if ( btnText.startsWith( "Grid" ) )
                {
                    //
                } else
                {
                    System.err.println( btnText + "not recognized" );
                }
            }
        }
    }


    /**
     * Graphic component initialization is all set here.
     */
    private void initComponents()
    {
        //Now populate our toolbar:
        mouseCoords = new JLabel( Bundle.LBL_Coords( 0 , 0 ) );
        this.add( mouseCoords );
        this.add( colorChooser );

        colorChooser.addActionListener( this );

        // Border visibility button & variable.
        this.borderVisible = false;
        this.borderToggle = new JButton( this.getBorderButtonText() );
        this.borderToggle.addActionListener( new BorderToggleBtn() );
        this.add( this.borderToggle );

        // Grid visibility toggle button
        this.gridToggle = new JButton( this.getGridButtonText() );
        this.gridToggle.addActionListener( new GridToggleBtn() );
        this.add( this.gridToggle );

        // Generic JButton that we'll use to instance several buttons.
        JButton genericBtn;

        //
        genericBtn = new JButton( "Print PDF" );
        genericBtn.addActionListener( new PrintPDFBtn() );
        this.add( genericBtn );

        //
        genericBtn = new JButton( "Preview" );
        genericBtn.addActionListener( new PreviewBtnAction() );
        this.add( genericBtn );
    }


    /**
     *
     * @return
     */
    private String getBorderButtonText()
    {
        if ( this.borderVisible )
        {
            this.borderVisible = false;
            return "Border OFF";
        } else
        {
            this.borderVisible = true;
            return "Border ON";
        }
    }


    /**
     *
     * @return
     */
    private String getGridButtonText()
    {
        if ( this.isGridVisible() )
        {
            return "Grid OFF";
        } else
        {
            return "Grid ON";
        }
    }


    /**
     *
     * @return true if the grid is visible, false otherwise.
     */
    private boolean isGridVisible()
    {
        if ( this.canvas != null )
        {
            return this.canvas.getGridVisible();
        }
        return false;
    }


    /**
     *
     * @param canvas
     */
    public void setCanvas( Canvas canvas )
    {
        this.canvas = canvas;
    }


    /**
     *
     * @param x
     * @param y
     */
    public void setMouseCoords( int x , int y )
    {
        this.mouseCoords.setText( Bundle.LBL_Coords( x , y ) );
    }


    /**
     *
     */
    private void toggleBorderVisible()
    {
        this.canvas.setBordersVisible( borderVisible );
        this.borderToggle.setText( this.getBorderButtonText() );
    }


    /**
     *
     */
    private void toggleGridVisible()
    {
        boolean currentGrid = this.isGridVisible();
        if ( currentGrid )
        {
            // the grid IS currently visible, let's turn it OFF
            this.canvas.setGridVisible( false );
        } else
        {
            // the grid IS NOT currently visible, let's turn it ON
            this.canvas.setGridVisible( true );
        }
        this.gridToggle.setText( this.getGridButtonText() );
    }


    /**
     *
     */
    private class BorderToggleBtn implements ActionListener
    {

        /**
         *
         * @param e
         */
        @Override
        public void actionPerformed( ActionEvent e )
        {
            //
            toggleBorderVisible();
        }

    }


    /**
     *
     */
    private class GridToggleBtn implements ActionListener
    {

        /**
         *
         * @param e
         */
        @Override
        public void actionPerformed( ActionEvent e )
        {
            toggleGridVisible();
        }

    }


    /**
     *
     */
    private class PrintPDFBtn implements ActionListener
    {

        /**
         *
         * @param e
         */
        @Override
        public void actionPerformed( ActionEvent e )
        {
            System.out.println( "Creating PDF" );
        }

    }


    /**
     *
     */
    private class PreviewBtnAction implements ActionListener
    {

        /**
         *
         * @param e
         */
        @Override
        public void actionPerformed( ActionEvent e )
        {
            System.out.println( "Safe!" );
        }

    }

}
